﻿using System.Linq;
using PropertyExpression.Common;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;

namespace PropertyExpression.ControllableQuery.Demo
{
    class Program
    {
        static void Main()
        {
            var tree = SyntaxTree.ParseText(@"using System;
using PropertyExpression.Common;

namespace PropertyExpression.ControllableQuery.Demo
{
    class SomeClass2
    {
        void Method1()
        {
            Filter? filter = Filter.Suggested;
            Option<string> search = ""%test%"";
            var userIsAnonymous = false;
            var userIsModerator = false;
            var userId = 1;
            var tagScoreRequiredToVote = 10;
            var tab = Tab.Newest;
            var start = 10;
            var finish = 20;
            var paging = BatchQuery<Paging<ITagSynonym, int?>>._ew(new {}, () => {
                WriteLiteral(""\r\n"");


#line 2 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                Write(Paging.Get(mainQuery: item => new PropertyExpression.ControllableQuery.SubQuery(__razor_template_writer => {


#line default
#line hidden
                    PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, @""
SELECT  TagSynonyms.Id, SourceTagName, TargetTagName, OwnerUserId, ApprovalDate, TagSynonyms.CreationDate,
        AutoRenameCount, Score, SourceTags.Id AS SourceTagId, TargetTags.Id AS TargetTagId, Users.DisplayName
FROM    TagSynonyms
        LEFT JOIN Tags SourceTags ON SourceTagName = SourceTags.Name
        LEFT JOIN Tags TargetTags ON TargetTagName = TargetTags.Name
        LEFT JOIN Users ON Users.Id = OwnerUserId
WHERE   1 = 1
"");


#line 10 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                    switch (filter)
                    {
                        case Filter.Active:


#line default
#line hidden
                            PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""        "");

                            PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""AND ApprovalDate IS NOT NULL\r\n"");


#line 13 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                            break;
                        case Filter.Suggested:


#line default
#line hidden
                            PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""        "");

                            PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""AND ApprovalDate IS NULL\r\n"");


#line 16 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                            if (!userIsAnonymous && !userIsModerator)
                            {


#line default
#line hidden
                                PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""        "");

                                PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""AND TargetTagName IN ("");


#line 17 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                                PropertyExpression.ControllableQuery.QWriter.WriteTo(__razor_template_writer, TargetTagNames());


#line default
#line hidden
                                PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, "")\r\n"");


#line 18 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                            }
                            break;
                        case Filter.Merge:


#line default
#line hidden
                            PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""        "");

                            PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""AND ApprovalDate IS NOT NULL AND ISNULL(SourceTags.Count, 0) > 0\r\n"");


#line 22 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                            break;
                    }


#line default
#line hidden

#line 24 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                    if (search.HasValue)
                    {


#line default
#line hidden
                        PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""        "");

                        PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""AND (SourceTagName LIKE "");


#line 25 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                        PropertyExpression.ControllableQuery.QWriter.WriteTo(__razor_template_writer, search);


#line default
#line hidden
                        PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, "" OR TargetTagName LIKE "");


#line 25 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                        PropertyExpression.ControllableQuery.QWriter.WriteTo(__razor_template_writer, search);


#line default
#line hidden
                        PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, "")\r\n"");


#line 26 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                    }

#line default
#line hidden

#line 26 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                }),
                    orderBy: item => new PropertyExpression.ControllableQuery.SubQuery(__razor_template_writer => {


#line default
#line hidden
                        PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""\r\n"");


#line 28 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                        switch (tab)
                        {
                            case Tab.Newest:


#line default
#line hidden
                                PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""        "");

                                PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""CreationDate DESC\r\n"");


#line 31 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                                break;
                            case Tab.Master:


#line default
#line hidden
                                PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""        "");

                                PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""TargetTagName ASC, AutoRenameCount DESC\r\n"");


#line 34 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                                break;
                            case Tab.Synonym:


#line default
#line hidden
                                PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""        "");

                                PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""SourceTagName ASC, AutoRenameCount DESC\r\n"");


#line 37 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                                break;
                            case Tab.Votes:


#line default
#line hidden
                                PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""        "");

                                PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""Score DESC, TargetTagName ASC, AutoRenameCount DESC\r\n"");


#line 40 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                                break;
                            case Tab.Creator:


#line default
#line hidden
                                PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""        "");

                                PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""DisplayName\r\n"");


#line 43 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                                break;
                            case Tab.Renames:


#line default
#line hidden
                                PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""        "");

                                PropertyExpression.ControllableQuery.QWriter.WriteLiteralTo(__razor_template_writer, ""AutoRenameCount DESC, TargetTagName\r\n"");


#line 46 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                                break;
                        }

#line default
#line hidden

#line 47 ""e:\Projects\PropertyExpression\Tfs\ControllableQuery\DevBranches\Razor4.Demo.UserSearch.Closure\Source\ControllableQuery.Demo\SomeClass2_12.cshtml""
                    }),
                    start: @start, finish: @finish));


#line default
#line hidden
                WriteLiteral(""\r\n"");

            }).Result();
            var rows = paging.Rows;
            var count = paging.Count;
        }
    }
}");
            var compilation = Compilation.Create(
                outputName: "HelloWorld",
                syntaxTrees: new [] {tree},
                references: new[] {
                    MetadataReference.CreateAssemblyReference("mscorlib"),
                    MetadataReference.CreateAssemblyReference("System.Data"),
                    new MetadataFileReference(typeof (Option<>).Assembly.Location),
                    new MetadataFileReference(typeof (Query<>).Assembly.Location),
                });
            var statementSyntax = (BlockSyntax) tree.GetRoot()
                .DescendantNodes()
                .OfType<MethodDeclarationSyntax>()
                .First().DescendantNodes()
                .OfType<BlockSyntax>()
                .Take(2).Last();
            var semanticModel = compilation.GetSemanticModel(tree);
            var analyzeDataFlow = semanticModel.AnalyzeDataFlow(statementSyntax);
            var dataFlowsIn = analyzeDataFlow.DataFlowsIn;
        }
    }
}
